using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Stormwind.Core.Contracts {
	/// <summary>
	/// User contract.
	/// </summary>
	[Serializable]
	public class User {
		/// <summary>
		/// Default Constructor.
		/// </summary>
		public User() {
		}
		/// <summary>
		/// Creates the contract based in a MembershipUser.
		/// </summary>
		/// <param name="user">Instance.</param>
		public User(object user) {
			this.Comment = GetValue<string>(user, "Comment");
			this.CreationDate = GetValue<DateTime>(user, "CreationDate");
			this.Email = GetValue<string>(user, "Email");
			this.IsApproved = GetValue<bool>(user, "IsApproved");
			this.IsLockedOut = GetValue<bool>(user, "IsLockedOut");
			this.IsOnline = GetValue<bool>(user, "IsOnline");
			this.LastActivityDate = GetValue<DateTime>(user, "LastActivityDate");
			this.LastLockoutDate = GetValue<DateTime>(user, "LastLockoutDate");
			this.LastLoginDate = GetValue<DateTime>(user, "LastLoginDate");
			this.LastPasswordChangedDate = GetValue<DateTime>(user, "LastPasswordChangedDate");
			this.PasswordQuestion = GetValue<string>(user, "PasswordQuestion");
			this.ProviderName = GetValue<string>(user, "ProviderName");
			this.UserName = GetValue<string>(user, "UserName");
		}

		/// <summary>
		/// Gets a value from a property.
		/// </summary>
		/// <typeparam name="T">Type of the property.</typeparam>
		/// <param name="obj">Object.</param>
		/// <param name="prop">Property name.</param>
		/// <returns>Returns the value.</returns>
		private T GetValue<T>(object obj, string prop) {
			Type tp = obj.GetType();
			PropertyInfo pi = tp.GetProperty(prop);
			if (pi == null) {
				throw new ArgumentNullException("The property " + prop + " was not found.");
			}
			return (T)pi.GetValue(obj, null);
		}

		private string comment;
		private DateTime creationDate;
		private string email;
		private bool isApproved;
		private bool isLockedOut;
		private bool isOnline;
		private DateTime lastActivityDate;
		private DateTime lastLockoutDate;
		private DateTime lastLoginDate;
		private DateTime lastPasswordChangedDate;
		private string passwordQuestion;
		private string providerName;
		private string userName;

		/// <summary>
		/// Comment.
		/// </summary>
		public string Comment {
			get {
				return comment;
			}
			set {
				comment = value;
			}
		}

		/// <summary>
		/// Creation date.
		/// </summary>
		public DateTime CreationDate {
			get {
				return creationDate;
			}
			set {
				creationDate = value;
			}
		}
		/// <summary>
		/// Email.
		/// </summary>
		public string Email {
			get {
				return email;
			}
			set {
				email = value;
			}
		}
		/// <summary>
		/// Indicates if this user is approved.
		/// </summary>
		public bool IsApproved {
			get {
				return isApproved;
			}
			set {
				isApproved = value;
			}
		}

		/// <summary>
		/// Indicates if this user is locked.
		/// </summary>
		public bool IsLockedOut {
			get {
				return isLockedOut;
			}
			set {
				isLockedOut = value;
			}
		}

		/// <summary>
		/// Indicates if this user is online.
		/// </summary>
		public bool IsOnline {
			get {
				return isOnline;
			}
			set {
				isOnline = value;
			}
		}

		/// <summary>
		/// Last activity date of this user.
		/// </summary>
		public DateTime LastActivityDate {
			get {
				return lastActivityDate;
			}
			set {
				lastActivityDate = value;
			}
		}

		/// <summary>
		/// Last lockout date for this user.
		/// </summary>
		public DateTime LastLockoutDate {
			get {
				return lastLockoutDate;
			}
			set {
				lastLockoutDate = value;
			}
		}

		/// <summary>
		/// Last login date for this user.
		/// </summary>
		public DateTime LastLoginDate {
			get {
				return lastLoginDate;
			}
			set {
				lastLoginDate = value;
			}
		}

		/// <summary>
		/// Last password change for this user.
		/// </summary>
		public DateTime LastPasswordChangedDate {
			get {
				return lastPasswordChangedDate;
			}
			set {
				lastPasswordChangedDate = value;
			}
		}

		/// <summary>
		/// Password question for this user.
		/// </summary>
		public string PasswordQuestion {
			get {
				return passwordQuestion;
			}
			set {
				passwordQuestion = value;
			}
		}

		/// <summary>
		/// Provider name for this user.
		/// </summary>
		public string ProviderName {
			get {
				return providerName;
			}
			set {
				providerName = value;
			}
		}

		/// <summary>
		/// Username.
		/// </summary>
		public string UserName {
			get {
				return userName;
			}
			set {
				userName = value;
			}
		}

	}
}
